﻿using System;
using System.Linq;
using System.Web.Mvc;
using BLL;
using Web.Areas.Manager.Models;

namespace Web.Areas.Manager.Controllers
{
    [UserAuthorize]
    public class EfficiencyLogReportController : Controller
    {
        EfficiencyLog_BLL _EfficiencyLog_BLL = new EfficiencyLog_BLL();

        public virtual ActionResult Index(int? pageIndex, string keyword, DateTime? date)
        {

            if (date == null)
            {
                date = DateTime.Now;
            }

            ViewBag.Year = date.Value.Year;
            ViewBag.Month = date.Value.Month;

            var model = _EfficiencyLog_BLL.Get().Where(a => a.Sys_Users.UserName.Equals(User.Identity.Name));
            if (!string.IsNullOrEmpty(keyword))
            {
                //  model = model.Where(a => a.UserName.Contains(keyword));
            }

            var item = from p in model
                       where p.AddDate.Year == date.Value.Year && p.AddDate.Month == date.Value.Month
                       group p by p.AddDate.Day into g
                       select new EfficiencyLogReportModel
                       {
                           Key = g.Key,
                           PlanCount = g.Count(),
                           PlanTime = g.Sum(a => System.Data.Objects.EntityFunctions.DiffMinutes(a.StarDate, a.EndDate)),
                           complete = g.Where(a => a.Completed).Count() * 100 / g.Count()
                       };

            return View(item);
        }
    }
}
